package com.liangtian.mdm.material.service.impl;

import com.liangtian.mdm.base.utils.DateUtils;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.material.common.constant.Constants;
import com.liangtian.mdm.material.dao.WlwlDao;
import com.liangtian.mdm.material.dao.WlwlDisDao;
import com.liangtian.mdm.material.model.Wlwl;
import com.liangtian.mdm.material.model.WlwlDis;
import com.liangtian.mdm.material.service.WlwlDisService;
import com.liangtian.mdm.system.business.dao.SysBusinessDao;
import com.liangtian.mdm.system.business.model.SysBusiness;
import com.liangtian.mdm.system.mdmDis.target.model.DisService;
import com.liangtian.mdm.system.mdmDis.target.model.DisServiceCondition;
import com.liangtian.mdm.system.mdmDis.target.service.DispatchTargetManager;
import com.liangtian.mdm.system.parameter.service.SysParameterService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WlwlDisServiceImpl implements WlwlDisService {

    @Autowired
    private WlwlDisDao wlwlDisDao;
    @Autowired
    private DispatchTargetManager dispatchTargetManager;
    @Autowired
    private SysBusinessDao sysBusinessDao;
    @Autowired
    private SysParameterService sysParameterService;
    @Autowired
    private WlwlDao wlwlDao;
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;


    @Override
    public void insertWlwlDis(Wlwl wl, String actiontype) {
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);//操作用户
        SysBusiness sysb = new SysBusiness();
        sysb.setBusTable("WL_WL");
        List<SysBusiness> bList = sysBusinessDao.queryBusByContion(sysb);//查询业务类型
        String bus_id = bList.get(0).getBusId();//业务类型
        DisServiceCondition conditon = new DisServiceCondition();
        conditon.setSurvey_type_id(bus_id);
        //conditon.setUnit_id("");
        if (user == null) {//万达工作流和OA集成，审核通过之后调用MDM服务端进行后续业务操作，所以获取当前用户为空
            conditon.setCreator_id("");
        } else {
            conditon.setCreator_id(user.getUserId() + "");
        }
        List<DisService> services = dispatchTargetManager.query_disService(conditon);
        if (!CollectionUtils.isEmpty(services)) {
            List<WlwlDis> list = new ArrayList<WlwlDis>();
            for (DisService se : services) {
                WlwlDis dis = new WlwlDis();
                dis.setSysId(se.getTarget_id());
                dis.setSysCode(se.getTarget_code());
                dis.setWlbm(wl.getWlbm());
                dis.setQc(wl.getQc());
                dis.setJc(wl.getJc());
                dis.setJldw(wl.getJldw());
                //dis.setWllx("ZMAT");
                dis.setGyms(wl.getCglx());
                dis.setLbbm(wl.getLbbm());
                dis.setLbmc(wl.getLbmc());
                dis.setDm(wl.getDm());
                dis.setTm(wl.getTm());
                if (actiontype.equals("4")) {
                    dis.setStatus("ZZ");
                }
                dis.setCreatorId(wl.getCreatorId());
                dis.setCreatorAccount(wl.getCreatorAccount());
                dis.setCreatorName(wl.getCreatorName());
                dis.setCreatorTime(wl.getCreateTime());
                dis.setGsdm(wl.getGsdm());     //公司代码是申请人所属的公司，是否修改成申请人所属的二级或三级单位
                dis.setGsmc(wl.getGsmc());
                dis.setState("0");
                dis.setActiontype(actiontype);  //操作类型  1:新增，2:修改，3:启用，4:停用
                dis.setSurveyType(Constants.WLWL);

                //晋煤 物料全描述 分发装备公司物料以及有标识的物料
                dis.setExtend1(wl.getField16());
                dis.setExtend2(wl.getCreatorId());
                dis.setExtend3(wl.getField1());

                list.add(dis);
            }
            //把参数封装在Map中
            Map<String, List<WlwlDis>> map = new HashMap<String, List<WlwlDis>>();
            map.put("wlwlDislist", list);
            wlwlDisDao.batchInsertWlwlDis(map);
        }
    }

    @Override
    public String[] manualWlwlDis(String wlbms) {
        String[] names = null;
        String[] wlbmArr = wlbms.split(",");
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);//操作用户
        SysBusiness sysb = new SysBusiness();
        sysb.setBusTable("WL_WL");
        List<SysBusiness> bList = sysBusinessDao.queryBusByContion(sysb);//查询业务类型
        String bus_id = bList.get(0).getBusId();//业务类型
        DisServiceCondition conditon = new DisServiceCondition();
        conditon.setSurvey_type_id(bus_id);
        conditon.setUnit_id("");
        conditon.setCreator_id(user.getUserId() + "");
        List<DisService> services = dispatchTargetManager.query_disService(conditon);
        if (!CollectionUtils.isEmpty(services)) {
            names = new String[services.size()];
            List<WlwlDis> list = new ArrayList<WlwlDis>();
            //--edit by wangyk at 180519:这里之前是单条分发物料，改成批量分发，里面的分发逻辑没变。
            for (String wlbm : wlbmArr) {
                Wlwl wl = wlwlDao.getWlwlByWlbm(wlbm);
                Map<String, Object> cond = new HashMap<>();
                for (int i = 0; i < services.size(); i++) {
                    DisService se = services.get(i);
                    names[i] = se.getTarget_name();
                    WlwlDis dis = new WlwlDis();
                    dis.setSysId(se.getTarget_id());
                    dis.setSysCode(se.getTarget_code());
                    dis.setWlbm(wl.getWlbm());
                    dis.setQc(wl.getQc());
                    dis.setJc(wl.getJc());
                    dis.setJldw(wl.getJldw());
                    //dis.setWllx("ZMAT");
                    dis.setGyms(wl.getCglx());
                    dis.setLbbm(wl.getLbbm());
                    dis.setLbmc(wl.getLbmc());
                    dis.setDm(wl.getDm());
                    dis.setTm(wl.getTm());
                    dis.setCreateTime(wl.getCreateTime());
                    dis.setCreatorId(user.getUserId() + "");
                    dis.setCreatorAccount(user.getUserAccount());
                    dis.setCreatorName(user.getUserName());
                    //dis.setCreate_time(new Date());
                    dis.setDownAccount(user.getUserAccount());
                    dis.setDownName(user.getUserName());
                    dis.setDownTime(new Date().toString());
                    dis.setDownGsdm("");
                    dis.setDownGsmc("");
                    dis.setGsdm(wl.getGsdm());
                    dis.setGsmc(wl.getGsmc());
                    dis.setState("0");
                    //晋煤 物料全描述 分发装备公司物料以及有标识的物料
                    dis.setExtend1(wl.getField16());
                    dis.setExtend2(wl.getCreatorId());
                    dis.setExtend3(wl.getField1());
                    cond.put("sys_id", se.getTarget_id());
                    cond.put("wlbm", wl.getWlbm());
//					if(wlwlDisDao.queryWlwlDisCount(cond)>0){
//						dis.setActiontype("2");
//					}else{
//						dis.setActiontype("1");
//					}
                    dis.setActiontype(wl.getActiontype());
                    dis.setCreatorTime(DateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    dis.setSurveyType(Constants.WLWL);
                    list.add(dis);
                }
            }
            //把参数手动封装在Map中
            Map<String, List<WlwlDis>> map = new HashMap<String, List<WlwlDis>>();
            map.put("wlwlDislist", list);
            wlwlDisDao.batchInsertWlwlDis(map);
        }
        return names;
    }

    @Override
    public List<WlwlDis> queryWlwldis(Map<String, Object> cond) {
        return wlwlDisDao.queryWlwlDis(cond);
    }
}
